#!/bin/bash
# 文件名: sync_data.sh

LOCKFILE="/tmp/sync_data.lock"
SRC_HOST="223.85.98.189"
SRC_PATH="/nfs/data/"
DST_PATH="/mnt/am-data/"
USER="sumzcp"
PASSWORD="XXXXXxxxx"

# 程序绝对路径
SSHPASS=/usr/bin/sshpass
RSYNC=/usr/bin/rsync
DATE=/usr/bin/date
FLOCK=/usr/bin/flock

exec 200>$LOCKFILE
$FLOCK -n 200 || {
    echo "已有同步任务在运行，退出。"
    exit 1
}

echo "开始同步: $($DATE)"

$SSHPASS -p "$PASSWORD" $RSYNC -avzP \
    -e "ssh -o StrictHostKeyChecking=no" \
    ${USER}@${SRC_HOST}:${SRC_PATH} \
    ${DST_PATH}

echo "同步完成: $($DATE)"
